//
//  HotCell.m
//  Everest
//
//  Created by Liu Chengbo on 14/12/24.
//  Copyright (c) 2014年 ThreeL. All rights reserved.
//

#import "HotCell.h"
#import "HotPresent.h"
#import "UIImageView+WebCache.h"
#import "UIColor+AddColor.h"


@interface HotCell ()
//@property (nonatomic, retain) UIView *bigView;
@property (nonatomic, retain) UILabel *titleLabel;

@property (nonatomic, retain) UIView *twoView;
@property (nonatomic, retain) UIView *byView;
@property (nonatomic, retain) UILabel *byLabel;
@property (nonatomic, retain) UILabel *nameLabel;

@property (nonatomic, retain) UIView *dayView;
@property (nonatomic, retain) UILabel *dayLabel;

@property (nonatomic, retain) UIImageView *playImageView;
@property (nonatomic, retain) UIImageView *heartImageView;
@property (nonatomic, retain) UIImageView *talkImageView;
@property (nonatomic, retain) UIImageView *downImageView;
@property (nonatomic, retain) UIImageView *buildTimeImageView;
@property (nonatomic, retain) UILabel *playLabel;
@property (nonatomic, retain) UILabel *heartLabel;
@property (nonatomic, retain) UILabel *talkLabel;
@property (nonatomic, retain) UILabel *downLabel;
@property (nonatomic, retain) UILabel *buildTimeLabel;
//@property (nonatomic, retain) UIView *linkView;

@property (nonatomic, retain) UIView *fiveView;
@property (nonatomic, retain) UIView *playView;
@property (nonatomic, retain) UIView *heartView;
@property (nonatomic, retain) UIView *talkView;
@property (nonatomic, retain) UIView *downView;
@property (nonatomic, retain) UIView *buildTimeView;

@property (nonatomic, retain) UIImageView *bigImageView;

@property (nonatomic, retain) UIButton *playButton;
@end

@implementation HotCell
- (void)dealloc
{
    //    [_bigView release];
    [_titleLabel release];
    [_twoView release];
    [_byView release];
    [_byLabel release];
    [_nameLabel release];
    [_dayView release];
    [_dayLabel release];
    [_playImageView release];
    [_heartImageView release];
    [_talkImageView release];
    [_downImageView release];
    [_buildTimeImageView release];
    [_playLabel release];
    [_heartLabel release];
    [_talkLabel release];
    [_downLabel release];
    [_buildTimeLabel release];
    //    [_linkView release];
    [_playView release];
    [_heartView release];
    [_talkView release];
    [_downView release];
    [_buildTimeView release];
    [_fiveView release];
    [_bigImageView release];
    [_playButton release];
    [_hotPre release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //        self.bigView = [[UIImageView alloc] init];
        //        [self.contentView addSubview:self.bigView];
        //        self.bigView.backgroundColor = [UIColor blueColor];
        //        [self.bigView release];
        
        self.fiveView = [[UIView alloc] init];
        [self.contentView addSubview:self.fiveView];
        //        self.fiveView.backgroundColor = [UIColor cyanColor];
        [self.fiveView release];
        
        self.playView = [[UIView alloc] init];
        [self.fiveView addSubview:self.playView];
        //        self.playView.backgroundColor = [UIColor redColor];
        [self.playView release];
        
        self.playImageView = [[UIImageView alloc] init];
        [self.playView addSubview:self.playImageView];
        //        self.playImageView.backgroundColor = [UIColor blueColor];
        [self.playImageView release];
        
        self.playLabel = [[UILabel alloc] init];
        [self.playView addSubview:self.playLabel];
        //        self.playLabel.backgroundColor = [UIColor greenColor];
        [self.playLabel release];
        
        self.heartView = [[UIView alloc] init];
        [self.fiveView addSubview:self.heartView];
        //        self.heartView.backgroundColor = [UIColor orangeColor];
        [self.heartView release];
        
        self.heartImageView = [[UIImageView alloc] init];
        [self.heartView addSubview:self.heartImageView];
        //        self.heartImageView.backgroundColor = [UIColor blueColor];
        [self.heartImageView release];
        
        self.heartLabel = [[UILabel alloc] init];
        [self.heartView addSubview:self.heartLabel];
        //        self.heartLabel.backgroundColor = [UIColor greenColor];
        [self.heartLabel release];
        
        self.talkView = [[UIView alloc] init];
        [self.fiveView addSubview:self.talkView];
        [self.talkView release];
        
        self.talkImageView = [[UIImageView alloc] init];
        [self.talkView addSubview:self.talkImageView];
        [self.talkImageView release];
        
        self.talkLabel = [[UILabel alloc] init];
        [self.talkView addSubview:self.talkLabel];
        [self.talkLabel release];
        
        self.downView = [[UIView alloc] init];
        [self.fiveView addSubview:self.downView];
        [self.downView release];
        
        self.downImageView = [[UIImageView alloc] init];
        [self.downView addSubview:self.downImageView];
        [self.downImageView release];
        
        self.downLabel = [[UILabel alloc] init];
        [self.downView addSubview:self.downLabel];
        [self.downLabel release];
        
        self.buildTimeView = [[UIView alloc] init];
        [self.fiveView addSubview:self.buildTimeView];
        //        self.buildTimeView.backgroundColor = [UIColor yellowColor];
        [self.buildTimeView release];
        
        self.buildTimeImageView = [[UIImageView alloc] init];
        [self.buildTimeView addSubview:self.buildTimeImageView];
        //        self.buildTimeImageView.backgroundColor = [UIColor blueColor];
        [self.buildTimeImageView release];
        
        self.buildTimeLabel = [[UILabel alloc] init];
        [self.buildTimeView addSubview:self.buildTimeLabel];
        //        self.buildTimeLabel.backgroundColor = [UIColor greenColor];
        [self.buildTimeLabel release];
        
        self.titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.titleLabel];
        //        self.titleLabel.backgroundColor = [UIColor blueColor];
        [self.titleLabel release];
        
        self.twoView = [[UIView alloc] init];
        [self.contentView addSubview:self.twoView];
        //        self.twoView.backgroundColor = [UIColor greenColor];
        [self.twoView release];
        
        self.byView = [[UIView alloc] init];
        [self.twoView addSubview:self.byView];
        //        self.byView.backgroundColor = [UIColor cyanColor];
        [self.byView release];
        
        self.byLabel = [[UILabel alloc] init];
        [self.byView addSubview:self.byLabel];
        //        self.byLabel.backgroundColor = [UIColor redColor];
        [self.byLabel release];
        
        self.nameLabel = [[UILabel alloc] init];
        [self.byView addSubview:self.nameLabel];
        //        self.nameLabel.backgroundColor = [UIColor orangeColor];
        [self.nameLabel release];
        
        self.dayView = [[UIView alloc] init];
        [self.twoView addSubview:self.dayView];
        //        self.mView.backgroundColor = [UIColor magentaColor];
        [self.dayView release];
        
        self.dayLabel = [[UILabel alloc] init];
        [self.dayView addSubview:self.dayLabel];
        //        self.sizeLabel.backgroundColor = [UIColor purpleColor];
        [self.dayLabel release];
        
        self.bigImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.bigImageView];
        //        self.bigImageView.backgroundColor = [UIColor grayColor];
        [self.bigImageView release];
        
    }
    return self;
}

- (void)setHotPre:(HotPresent *)hotPre
{
    if (_hotPre != hotPre) {
        [_hotPre release];
        _hotPre = [hotPre retain];
    }
    // 给"标题"赋值
    self.titleLabel.text = self.hotPre.title;
    // 给"发布者名字"赋值
    self.nameLabel.text = self.hotPre.nickname;
    // 给"cell右侧图片"赋值
    [self.bigImageView setImageWithURL:[NSURL URLWithString:self.hotPre.coverSmall]];
    // 给"播放量"赋值
    float play = [self.hotPre.playtimes floatValue];
    if (play < 10000) {
        self.playLabel.text = [NSString stringWithFormat:@"%.0f", play];
    } else if (play > 10000) {
        self.playLabel.text = [NSString stringWithFormat:@"%.1f万", [self.hotPre.playtimes floatValue] / 10000];
    }
    // 给"喜欢"赋值
    int heart = [self.hotPre.likes intValue];
    self.heartLabel.text = [NSString stringWithFormat:@"%d", heart];
    // 给"讨论"赋值
    int talk = [self.hotPre.comments intValue];
    self.talkLabel.text = [NSString stringWithFormat:@"%d", talk];
    // 给"分享次数"赋值
    int share = [self.hotPre.shares intValue];
    self.downLabel.text = [NSString stringWithFormat:@"%d", share];
    // 给"时长"赋值
    CGFloat times = [self.hotPre.duration floatValue];
    self.buildTimeLabel.text = [self convertTime:times];
    
    // 给"几天前"赋值
    NSDate *now = [NSDate date];
    NSTimeInterval stime = [now timeIntervalSince1970];
    NSString *tempstr = [self.hotPre.createdAt description];
    NSString *istr = [tempstr substringToIndex:10];
    int i = [istr intValue];
    int iday = (stime - i) / (60 * 60 * 24);
    
    if (iday < 1) {
        self.dayLabel.text = [NSString stringWithFormat:@"%.0f小时前", (stime - i) / (60 * 60)];
    } else if (iday < 30) {
        self.dayLabel.text = [NSString stringWithFormat:@"%d天前", iday];
    } else if (iday > 30) {
        int month = iday / 30;
        self.dayLabel.text = [NSString stringWithFormat:@"%d个月前", month];
    }
}
// 计算时长的方法
- (NSString *)convertTime:(CGFloat)second{
    NSDate *d = [NSDate dateWithTimeIntervalSince1970:second];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    if (second / 3600 >= 1) {
        [formatter setDateFormat:@"HH:mm:ss"];
    } else {
        [formatter setDateFormat:@"mm:ss"];
    }
    NSString *showtimeNew = [formatter stringFromDate:d];
    [formatter release];
    return showtimeNew;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    // cell右侧图片
    self.bigImageView.frame = CGRectMake(kCellWidth / 32 * 22, self.contentView.bounds.size.height / 92 * 9, kCellWidth / 6, kCellWidth / 6);
    // cell标题(文字)
    self.titleLabel.frame = CGRectMake(kCellWidth / 10, self.contentView.bounds.size.height / 10, kCellWidth / 30 * 17, 0);
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.titleLabel sizeToFit];
    
    // twoView (已自适应)
    self.twoView.frame = CGRectMake(kCellWidth / 10, self.contentView.bounds.size.height / 40 * 25, kCellWidth / 30 * 17, 15);
    
    // By
    self.byView.frame = CGRectMake(0, 0, self.twoView.frame.size.width / 9 * 7, 15);
    // 发布者(图标)
    self.byLabel.frame = CGRectMake(0, 0, self.byView.frame.size.width / 8 * 1, 15);
    self.byLabel.text = @"by";
    self.byLabel.font = [UIFont systemFontOfSize:12];
    self.byLabel.textColor = [UIColor grayColor];
    // 发布者(文字)
    self.nameLabel.frame = CGRectMake(self.byView.frame.size.width / 8 * 1, 0, self.byView.frame.size.width / 8 * 6, 15);
    self.nameLabel.font = [UIFont systemFontOfSize:12];
    self.nameLabel.textAlignment = NSTextAlignmentLeft;
    self.nameLabel.textColor = [UIColor grayColor];
    
    // 多少天之前发布的状态
    self.dayView.frame = CGRectMake(self.twoView.frame.size.width / 9 * 7, 0, self.twoView.frame.size.width / 9 * 2, 15);
    // 几天前(文字)
    self.dayLabel.frame = CGRectMake(- self.byView.frame.size.width / 8 * 1, 0, (self.twoView.frame.size.width / 9 * 2) + self.byView.frame.size.width / 8 * 1, 15);
    self.dayLabel.font = [UIFont systemFontOfSize:12];
    self.dayLabel.textColor = [UIColor grayColor];
    self.dayLabel.textAlignment = NSTextAlignmentRight;
    
    // 五个小部件 (已自适应)
    self.fiveView.frame = CGRectMake(kCellWidth / 10, self.twoView.frame.origin.y + 25, kCellWidth / 10 * 7, 15);
    // 播放次数(图标)
    self.playView.frame = CGRectMake(-5, 0, self.fiveView.frame.size.width / 5, 15);
    self.playImageView.frame = CGRectMake(0, 0, self.playView.frame.size.width / 3, 15);
    self.playImageView.image = [UIImage imageNamed:@"iconfont-play.png"];
    // 播放次数(文字)
    self.playLabel.frame = CGRectMake(self.playView.frame.size.width / 3, 0, 5 + self.playView.frame.size.width / 3 * 2, 15);
    self.playLabel.font = [UIFont systemFontOfSize:10];
    self.playLabel.textColor = [UIColor grayColor];
    
    // 喜欢(图标)
    self.heartView.frame = CGRectMake(self.fiveView.frame.size.width / 5, 0, self.fiveView.frame.size.width / 5, 15);
    self.heartImageView.frame = CGRectMake(0, 0, self.heartView.frame.size.width / 3, 15);
    self.heartImageView.image = [UIImage imageNamed:@"iconfont-heart.png"];
    // 喜欢(文字)
    self.heartLabel.frame = CGRectMake(self.heartView.frame.size.width / 3, 0, self.heartView.frame.size.width / 3 * 2, 15);
    self.heartLabel.font = [UIFont systemFontOfSize:10];
    self.heartLabel.textColor = [UIColor grayColor];
    
    // 讨论(图标)
    self.talkView.frame = CGRectMake(self.fiveView.frame.size.width / 5 * 2, 0, self.fiveView.frame.size.width / 5, 15);
    self.talkImageView.frame = CGRectMake(0, 0, self.talkView.frame.size.width / 3, 15);
    self.talkImageView.image = [UIImage imageNamed:@"iconfont-pinglun.png"];
    // 讨论(文字)
    self.talkLabel.frame = CGRectMake(self.talkView.frame.size.width / 3, 0, self.talkView.frame.size.width / 3 * 2, 15);
    self.talkLabel.font = [UIFont systemFontOfSize:10];
    self.talkLabel.textColor = [UIColor grayColor];
    
    // 分享次数(图标)
    self.downView.frame = CGRectMake(self.fiveView.frame.size.width / 5 * 3, 0, self.fiveView.frame.size.width / 5, 15);
    self.downImageView.frame = CGRectMake(0, 0, self.downView.frame.size.width / 4, 15);
    self.downImageView.image = [UIImage imageNamed:@"iconfont-xiaofenxiang.png"];
    // 分享次数(文字)
    self.downLabel.frame = CGRectMake(self.downView.frame.size.width / 3, 0, self.downView.frame.size.width / 3 * 2, 15);
    self.downLabel.font = [UIFont systemFontOfSize:10];
    self.downLabel.textColor = [UIColor grayColor];
    
    // 时长(图标)
    self.buildTimeView.frame = CGRectMake((self.fiveView.frame.size.width / 5 * 4) - 10, 0, self.fiveView.frame.size.width / 5, 15);
    self.buildTimeImageView.frame = CGRectMake(0, 0, self.buildTimeView.frame.size.width / 3, 15);
    self.buildTimeImageView.image = [UIImage imageNamed:@"iconfont-shijiantime.png"];
    // 时长(文字)
    self.buildTimeLabel.frame = CGRectMake(self.buildTimeView.frame.size.width / 3, 0, 10 + self.buildTimeView.frame.size.width / 3 * 2, 15);
    self.buildTimeLabel.font = [UIFont systemFontOfSize:10];
    self.buildTimeLabel.textColor = [UIColor grayColor];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
